#include "Composition.h"
#include "CompositionPropertiesDialog.h"

CompositionPropertiesDialog::CompositionPropertiesDialog(HINSTANCE hInstance, Composition *composition) throw(): Dialog(hInstance, TEXT("Song properties"), CS_OKCANCEL), m_composition(composition) {
    CreateWindow(TEXT("STATIC"), TEXT("Beats per minute:"), WS_CHILD | WS_VISIBLE | SS_LEFT, widthToPixels(4), heightToPixels(4), widthToPixels(width() - 8), heightToPixels(8), hWnd(), 0, hInstance, 0);
    TCHAR buf[128];
    wsprintf(buf, TEXT("%d"), m_composition->beatsPerMinute());
    m_hBpm = CreateWindow(TEXT("EDIT"), buf, WS_BORDER | WS_CHILD | WS_VISIBLE | ES_NUMBER, widthToPixels(4), heightToPixels(16), widthToPixels(width() - 8), heightToPixels(12), hWnd(), 0, hInstance, 0);

    CreateWindow(TEXT("STATIC"), TEXT("Signature:"), WS_CHILD | WS_VISIBLE | SS_LEFT, widthToPixels(4), heightToPixels(36), widthToPixels(width() - 8), heightToPixels(8), hWnd(), 0, hInstance, 0);
    m_hSigBpm = CreateWindow(TEXT("COMBOBOX"), 0, WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS, widthToPixels(4), heightToPixels(48), widthToPixels(24), heightToPixels(64), hWnd(), 0, hInstance, 0);
    SendMessage(m_hSigBpm, CB_ADDSTRING, 0, LPARAM(TEXT("2")));
    SendMessage(m_hSigBpm, CB_ADDSTRING, 0, LPARAM(TEXT("3")));
    SendMessage(m_hSigBpm, CB_ADDSTRING, 0, LPARAM(TEXT("4")));
    SendMessage(m_hSigBpm, CB_ADDSTRING, 0, LPARAM(TEXT("6")));
    SendMessage(m_hSigBpm, CB_ADDSTRING, 0, LPARAM(TEXT("8")));
    switch(m_composition->beatsPerMeasure()) {
        case 2:
            SendMessage(m_hSigBpm, CB_SETCURSEL, 0, 0);
            break;
        case 3:
            SendMessage(m_hSigBpm, CB_SETCURSEL, 1, 0);
            break;
        case 4:
            SendMessage(m_hSigBpm, CB_SETCURSEL, 2, 0);
            break;
        case 6:
            SendMessage(m_hSigBpm, CB_SETCURSEL, 3, 0);
            break;
        case 8:
            SendMessage(m_hSigBpm, CB_SETCURSEL, 4, 0);
            break;
        default:
            ASSERT(!"Not allowed signature!");
            break;
    }
    CreateWindow(TEXT("STATIC"), TEXT("/"), WS_CHILD | WS_VISIBLE | SS_CENTER, widthToPixels(28), heightToPixels(50), widthToPixels(8), heightToPixels(8), hWnd(), 0, hInstance, 0);
    m_hSigBnt = CreateWindow(TEXT("COMBOBOX"), 0, WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS, widthToPixels(36), heightToPixels(48), widthToPixels(24), heightToPixels(64), hWnd(), 0, hInstance, 0);
    SendMessage(m_hSigBnt, CB_ADDSTRING, 0, LPARAM(TEXT("2")));
    SendMessage(m_hSigBnt, CB_ADDSTRING, 0, LPARAM(TEXT("4")));
    SendMessage(m_hSigBnt, CB_ADDSTRING, 0, LPARAM(TEXT("8")));
    SendMessage(m_hSigBnt, CB_ADDSTRING, 0, LPARAM(TEXT("16")));
    switch(m_composition->beatNoteType()) {
        case 2:
            SendMessage(m_hSigBnt, CB_SETCURSEL, 0, 0);
            break;
        case 4:
            SendMessage(m_hSigBnt, CB_SETCURSEL, 1, 0);
            break;
        case 8:
            SendMessage(m_hSigBnt, CB_SETCURSEL, 2, 0);
            break;
        case 16:
            SendMessage(m_hSigBnt, CB_SETCURSEL, 3, 0);
            break;
        default:
            ASSERT(!"Not allowed signature!");
            break;
    }
}

CompositionPropertiesDialog::~CompositionPropertiesDialog() {
    DestroyWindow(m_hSigBnt);
    DestroyWindow(m_hSigBpm);
    DestroyWindow(m_hBpm);
}

bool CompositionPropertiesDialog::close(CloseCommand cc) throw() {
    if(cc == CC_OK) {
        TCHAR buf[128];
        SendMessage(m_hBpm, WM_GETTEXT, 128, LPARAM(buf));
        m_composition->setBeatsPerMinute(_tcstol(buf, 0, 10));

        switch(SendMessage(m_hSigBpm, CB_GETCURSEL, 0, 0)) {
            case 0:
                m_composition->setBeatsPerMeasure(2);
                break;
            case 1:
                m_composition->setBeatsPerMeasure(3);
                break;
            case 2:
                m_composition->setBeatsPerMeasure(4);
                break;
            case 3:
                m_composition->setBeatsPerMeasure(6);
                break;
            case 4:
                m_composition->setBeatsPerMeasure(8);
                break;
            default:
                ASSERT(!"Not allowed signature!");
                break;
        }
        switch(SendMessage(m_hSigBnt, CB_GETCURSEL, 0, 0)) {
            case 0:
                m_composition->setBeatNoteType(2);
                break;
            case 1:
                m_composition->setBeatNoteType(4);
                break;
            case 2:
                m_composition->setBeatNoteType(8);
                break;
            case 3:
                m_composition->setBeatNoteType(16);
                break;
            default:
                ASSERT(!"Not allowed signature!");
                break;
        }
    }

    return(true);
}

void CompositionPropertiesDialog::controlNotification(HWND hWnd, WPARAM notification) throw() {
}

void CompositionPropertiesDialog::menuNotification(unsigned int itemId) throw() {
}
